package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.vo.admin.config.ExpressConfigVo;
import com.cskaoyan.bean.vo.admin.config.MallConfigVo;
import com.cskaoyan.bean.vo.admin.config.OrderConfigVo;
import com.cskaoyan.bean.vo.admin.config.WxConfigVo;
import com.cskaoyan.mapper.MarketSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

/**
 * /**
 *
 * @author 35291
 * @since 2022/11/19 17:27
 */
@Service
public class ConfigServiceImpl implements ConfigService{
    @Autowired
    MarketSystemMapper marketSystemMapper;
    @Override
    public MallConfigVo mallGetConfig() {
        // 先获得一个MallConfigVo
        MallConfigVo mallConfigVo = new MallConfigVo();
        try {
            // 再从vo里面获得class文件
            Class<? extends MallConfigVo> configVoClass = mallConfigVo.getClass();
            // mapper.selectByExample
            List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(null);
            // 再获得成员变量
            Field[] declaredFields = configVoClass.getDeclaredFields();
            // 进行遍历获得查询里面的值
            for (int i = 0; i < marketSystems.size(); i++) {
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    if (marketSystems.get(i).getKeyName().equalsIgnoreCase(declaredField.getName())){
                        declaredField.set(mallConfigVo,marketSystems.get(i).getKeyValue());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return mallConfigVo;
    }

    @Override
    public int mallPostConfig(MallConfigVo mallConfigVo) {
       // 获得vo对象
        MallConfigVo configVo = new MallConfigVo();
        // 获得vo里面的字节码对象
        Class<? extends MallConfigVo> configVoClass = configVo.getClass();
        // 在从字节码对象中获得成员变量
        Field[] declaredFields = configVoClass.getDeclaredFields();
        // 获取成员变量值的长度
        int i = declaredFields.length;
        for (Field declaredField : declaredFields) {
            // 忽略java语法
            declaredField.setAccessible(true);
            if (declaredField.getName().equals("market_mall_longitude")){
                i=i-marketSystemMapper.updateSystem(mallConfigVo.getMarket_mall_latitude(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_mall_latitude")){
                i=i-marketSystemMapper.updateSystem(mallConfigVo.getMarket_mall_longitude(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_mall_address")){
                i = i-marketSystemMapper.updateSystem(mallConfigVo.getMarket_mall_address(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_mall_qq")){
                i = i-marketSystemMapper.updateSystem(mallConfigVo.getMarket_mall_qq(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_mall_phone")){
                i= i-marketSystemMapper.updateSystem(mallConfigVo.getMarket_mall_phone(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_mall_name")){
                i= i-marketSystemMapper.updateSystem(mallConfigVo.getMarket_mall_name(),declaredField.getName());
            }
            if (i== declaredFields.length){
                return 600;
            }
        }
        return 200;
    }

    @Override
    public WxConfigVo wxGetConfig() {
        // 首先是获得一个wxConfigVo的对象
        WxConfigVo wxConfigVo = new WxConfigVo();
        try {
            // 获得wxConfigVo的class文件
            Class<? extends WxConfigVo> wxConfigVoClass = wxConfigVo.getClass();
            // 获得表中的数据
            List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(null);
            // 通过class文件获得成员变量
            Field[] declaredFields = wxConfigVoClass.getDeclaredFields();
            for (int i = 0; i < marketSystems.size(); i++) {
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    if (marketSystems.get(i).getKeyName().equalsIgnoreCase(declaredField.getName())){
                        declaredField.set(wxConfigVo,marketSystems.get(i).getKeyValue());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return wxConfigVo;
    }

    @Override
    public ExpressConfigVo expressConfigVoBaseRespVo() {
        ExpressConfigVo expressConfigVo = new ExpressConfigVo();
        try {
            // 首先通过反射获得所需要返回值的class文件
            Class<? extends ExpressConfigVo> expressConfigVoClass = expressConfigVo.getClass();
            // 通过查询获得表里面的内容
            List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(null);
            // 通过反射获得所有的成员变量的值
            Field[] declaredFields = expressConfigVoClass.getDeclaredFields();
            for (int i = 0; i < marketSystems.size(); i++) {
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    if (marketSystems.get(i).getKeyName().equalsIgnoreCase(declaredField.getName())){
                        declaredField.set(expressConfigVo,marketSystems.get(i).getKeyValue());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return expressConfigVo;
    }

    @Override
    public OrderConfigVo orderGetConfig() {
        // 首先创建一个Vo对象
        OrderConfigVo orderConfigVo = new OrderConfigVo();
        try {
            // 从Vo对象种获取class文件
            Class<? extends OrderConfigVo> orderConfigVoClass = orderConfigVo.getClass();
            // 然后查询表格里面所有的内容
            List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(null);
            // 通过vo的字节码对象获取所有成员变量
            Field[] declaredFields = orderConfigVoClass.getDeclaredFields();
            for (int i = 0; i < marketSystems.size(); i++) {
                for (Field declaredField : declaredFields) {
                    // 忽略java语法
                    declaredField.setAccessible(true);
                    if (marketSystems.get(i).getKeyName().equalsIgnoreCase(declaredField.getName())){
                        declaredField.set(orderConfigVo,marketSystems.get(i).getKeyValue());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return orderConfigVo;
    }

    @Override
    public int expressPostConfig(ExpressConfigVo expressConfigVo) {
        // 首先获得一个vo对象
        ExpressConfigVo expressConfigVos = new ExpressConfigVo();
        // 通过vo对象获得反射文件class
        Class<? extends ExpressConfigVo> expressConfigVoClass = expressConfigVos.getClass();
        // 通过反射文件获得成员变量
        Field[] declaredFields = expressConfigVoClass.getDeclaredFields();
        // 获得成员变量的长度
        int i = declaredFields.length;
        for (Field declaredField : declaredFields) {
            // 忽略java语法
            declaredField.setAccessible(true);
            if (declaredField.getName().equals("market_express_freight_min")){
                i=i-marketSystemMapper.updateSystem(expressConfigVo.getMarket_express_freight_value(),declaredField.getName());
            }
            if (declaredField.getName().equals(" market_express_freight_value")){
                i=i-marketSystemMapper.updateSystem(expressConfigVo.getMarket_express_freight_value(),declaredField.getName());
            }
            if (i== declaredFields.length){
                return 600;
            }
        }
        return 200;
    }

    @Override
    public int orderPostConfig(OrderConfigVo orderConfigVo) {
        // 首先new一个vo对象
        OrderConfigVo orderConfigVo1 = new OrderConfigVo();
        // 通过vo1获得class对象
        Class<? extends OrderConfigVo> orderConfigVo1Class = orderConfigVo1.getClass();
        // 通过class对象获得
        Field[] declaredFields = orderConfigVo1Class.getDeclaredFields();
        // 获得成员变量的长度
        int i = declaredFields.length;
        for (Field declaredField : declaredFields) {
            // 忽略java语法
            declaredField.setAccessible(true);
            if (declaredField.getName().equals("market_order_unconfirm")){
                i=i-marketSystemMapper.updateSystem(orderConfigVo.getMarket_order_unconfirm(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_order_unpaid")){
                i=i-marketSystemMapper.updateSystem(orderConfigVo.getMarket_order_unpaid(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_order_comment")){
                i = i-marketSystemMapper.updateSystem(orderConfigVo.getMarket_order_comment(),declaredField.getName());
            }
            if (i == declaredFields.length){
                return 600;
            }
        }
        return 200;
    }

    @Override
    public int wxPostConfig(WxConfigVo wxConfigVo) {
        // 首先获得一个Vo对象
        WxConfigVo wxConfigVo1 = new WxConfigVo();
        // 在从vo对象中获得class文件
        Class<? extends WxConfigVo> wxConfigVo1Class = wxConfigVo1.getClass();
        // 从class文件获得成员变量
        Field[] declaredFields = wxConfigVo1Class.getDeclaredFields();
        // 获得成员变量的长度
        int i = declaredFields.length;
        // 遍历所有成员变量
        for (Field declaredField : declaredFields) {
            // 忽略java语法
            declaredField.setAccessible(true);
            if (declaredField.getName().equals("market_wx_index_new")){
                i= i-marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_index_new(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_wx_index_topic")){
                i=i-marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_index_topic(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_wx_share")){
               i=i- marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_share(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_wx_index_brand")){
               i=i- marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_index_brand(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_wx_catlog_goods")){
                i=i-marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_catlog_goods(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_wx_catlog_list")){
                i=i-marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_catlog_list(),declaredField.getName());
            }
            if (declaredField.getName().equals("market_wx_index_hot")){
                i=i-marketSystemMapper.updateSystem(wxConfigVo.getMarket_wx_index_hot(),declaredField.getName());
            }
            if (i== declaredFields.length){
                return 600;
            }
        }
        return 200;
    }
}
